<?php session_start();?>
<?php

	$q = isset($_GET['id_resto']) ? $_GET['id_resto'] : null ;
	$intervalo =  isset($_GET['intervalo']) ? $_GET['intervalo'] : null ;
	$type_graph = $_GET['type'];
	$page_id =  isset($_GET['page_id']) ? $_GET['page_id'] : null ;
	
	if (!$q) return;
	
	set_include_path('../140dev/db');
	
	require_once('140dev_config.php');	  
	require_once('db_lib.php');
	
	$oDB = new db;
    
 	if(!$oDB)
  	{
  		die('Could not connect: ' . mysql_error());
  	}
	
	try{
		if($type_graph == "GET_ID_RESTO_BY_PAGE_ID"){
			
			
			$sql = "SELECT ID_RESTAURANT ";
			$sql = $sql . "FROM MAST_RESTAURANTS T1 INNER JOIN MAST_FACEBOOK_ACCOUNTS T2 ";
			$sql = $sql . "ON T1.DS_REST_FACEBOOK = T2.DS_USER_FB ";
			$sql = $sql . "WHERE T2.ID_USER_FB = '" . $page_id . "' ";
			
			$result = $oDB->select($sql);
			
			$result_arr = array();
			
			while($row = mysqli_fetch_row($result)){
				$result_values = array();
				$result_values['id_resto'] = $row[0];
				
				$values =  "ID_RESTAURANT=" . $row[0] . ",DT_TIMESTAMP=NOW(),DS_SOURCE='FACEBOOK_WIDGET', ID_CAMPAIGN=NULL";
				$oDB->insert('MAST_AUDIT_ACCESS',$values);
								
				array_push($result_arr,$result_values);
			}
			
			
			
			print json_encode($result_arr, JSON_NUMERIC_CHECK);	
			
		}
		if($type_graph == "SCORING_BY_CATEGORIES"){ 
			$sql = "SELECT T2.ID_TYPE_REST, T2.DS_TYPE_REST ";
			$sql = $sql . "FROM REL_TYPE_REST_MAST T1 ";
			$sql = $sql . "INNER JOIN MAST_TYPE_REST T2 ON T2.ID_TYPE_REST = T1.ID_TYPE_REST ";
			$sql = $sql . "WHERE ID_RESTAURANT = " . $q . " AND T2.ID_TYPE_REST <> 1 ";
			$sql = $sql . "ORDER BY T2.DS_TYPE_REST ASC";
			
			$result = $oDB->select($sql);
			
			$result_categories = array();
			$filter_categories = "";
			while($row = mysqli_fetch_row($result))
			{
				$result_category = array();
				$result_category['id'] = $row[0];
				$result_category['desc'] = $row[1];
				
				$filter_categories = $filter_categories . "ID_TYPE_REST = " . $row[0] . " OR ";
				array_push($result_categories,$result_category);
			}
			if(strlen($filter_categories) > 0)
				$filter_categories = substr($filter_categories , 0, strlen($filter_categories)-4);
				
				
			$sql = "SELECT DISTINCT T1.ID_RESTAURANT, T3.DS_REST_NAME, T1.NUM_VALUE ";
			$sql = $sql . "FROM LAST_VALUES_RESTAURANT T1  ";
			$sql = $sql . "INNER JOIN REL_TYPE_REST_MAST T2 ON ";
			$sql = $sql . "T1.ID_RESTAURANT = T2.ID_RESTAURANT ";
			$sql = $sql . "INNER JOIN MAST_RESTAURANTS T3 ON ";
			$sql = $sql . "T1.ID_RESTAURANT = T3.ID_RESTAURANT ";
			$sql = $sql . "INNER JOIN MAST_RESTAURANTS T4 ON ";
			$sql = $sql . "T3.ID_COUNTRY_3 = T4.ID_COUNTRY_3 AND ";
			$sql = $sql . "T3.ID_ADMIN_AREA_L1 = T4.ID_ADMIN_AREA_L1 AND ";
			$sql = $sql . "T3.ID_ADMIN_AREA_L2 = T4.ID_ADMIN_AREA_L2 AND ";
			$sql = $sql . "T3.ID_LOCALITY = T4.ID_LOCALITY ";
			$sql = $sql . "WHERE (" . $filter_categories . ") AND ";
			$sql = $sql . "ID_PARAMETER = 'NUM_SCORE_GLOBAL' AND ";
			$sql = $sql . "T4.ID_RESTAURANT = '" . $q . "' ";
			$sql = $sql . "ORDER BY NUM_VALUE DESC ";
			$sql = $sql . "LIMIT 5 ";
			
			$result = $oDB->select($sql);
			
			$result_arr = array();
			$result_des = array();
			$result_values = array();
			$esta_en_ranking = false;
			while($row = mysqli_fetch_row($result))
			{
				array_push($result_des,substr($row[1],0,22));
				// Si esta el ID lo marco en negro
				if($row[0]==$q){
					$resto_arr = array();
					$resto_arr['y'] = $row[2];
					$resto_arr['color'] = '#FBB040';
					array_push($result_values,$resto_arr);
					$esta_en_ranking = true;
				}
				else
					array_push($result_values,$row[2]);
			}
			
			if(!$esta_en_ranking){
				$sql = "SELECT DISTINCT T1.ID_RESTAURANT, T3.DS_REST_NAME, T1.NUM_VALUE ";
				$sql = $sql . "FROM LAST_VALUES_RESTAURANT T1  ";
				$sql = $sql . "INNER JOIN MAST_RESTAURANTS T3 ON ";
				$sql = $sql . "T1.ID_RESTAURANT = T3.ID_RESTAURANT ";
				$sql = $sql . "WHERE ";
				$sql = $sql . "T1.ID_PARAMETER = 'NUM_SCORE_GLOBAL' AND ";
				$sql = $sql . "T1.ID_RESTAURANT = " . $q;
			
				$result = $oDB->select($sql);
				while($row = mysqli_fetch_row($result))
				{
					// Puntos suspensivos
					array_push($result_des,"...");
					$resto_arr = array();
					$resto_arr['y'] = 0;
					$resto_arr['color'] = '#FBB040';
					array_push($result_values,$resto_arr);
					
					// Valor
					array_push($result_des,substr($row[1],0,22));
					$resto_arr = array();
					$resto_arr['y'] = $row[2];
					$resto_arr['color'] = '#FBB040';
					array_push($result_values,$resto_arr);
				}
			}
			
			array_push($result_arr,$result_des);
			array_push($result_arr,$result_values);
			array_push($result_arr,$result_categories);
			
			print json_encode($result_arr, JSON_NUMERIC_CHECK);
			
		}
		
		if($type_graph == "TWITTER_STATS"){
			$days_itervalo = 0;
			if($intervalo == '7D')
				$days_itervalo = 8;
			if($intervalo == '15D')
				$days_itervalo = 16;
			if($intervalo == '30D')
				$days_itervalo = 31;
			if($intervalo == '3M')
				$days_itervalo = 91;
			if($intervalo == '6M')
				$days_itervalo = 181;
			if($intervalo == '1A')
				$days_itervalo = 366;		
			$sql = "SELECT DATE_FORMAT(T2.DT_CREATED_AT, '%Y-%m-%d'),  COUNT(*) ";
			$sql = $sql . "FROM MAST_TWITTER_TWEETS T2 ";
			$sql = $sql . "INNER JOIN MAST_TWITTER_MENTIONS T3 ON T2.ID_TWEET = T3.ID_TWEET ";
			$sql = $sql . "INNER JOIN MAST_TWITTER_ACCOUNTS T4 ON T4.ID_USER_TW = T3.ID_TARGET_USER ";
			$sql = $sql . "INNER JOIN MAST_RESTAURANTS T5 ON T5.DS_REST_TWITTER = T4.DS_SCREEN_NAME ";
			$sql = $sql . "WHERE T5.ID_RESTAURANT = '" . $q . "' AND DATE_FORMAT(T2.DT_CREATED_AT, '%Y-%m-%d') > (NOW() - INTERVAL " . $days_itervalo . " DAY) ";
			$sql = $sql . "GROUP BY DATE_FORMAT(T2.DT_CREATED_AT, '%Y-%m-%d') ";
			$sql = $sql . "ORDER BY T2.DT_CREATED_AT ASC ";
			
			$result = $oDB->select($sql);
			
			$result_arr = array();
			$result_arr_values = array();
			$result_arr_fechas = array();
			$days = ($days_itervalo-1)*-1;
			
			while($row = mysqli_fetch_row($result))
			{
				
				$dt_score = date_create($row[0]);
				
				while(date("m.d.y",strtotime("$days day"))!=$dt_score->format('m.d.y')){
					
					array_push($result_arr_values,"0");	
					array_push($result_arr_fechas,date("d/m",strtotime("$days day")));
					$days = $days + 1;
				}
				
				
				array_push($result_arr_values,$row[1]);
				array_push($result_arr_fechas,$dt_score->format('d/m'));
				
				$days = $days + 1;
				
			}
			
			while($days<1){
				array_push($result_arr_values,"0");	
				array_push($result_arr_fechas,date("d/m",strtotime("$days day")));
				$days = $days + 1;
			}
			
			$sql = "SELECT DT_TIMESTAMP, NUM_FOLLOWERS ";
			$sql = $sql . "FROM MAST_RESTAURANTS T1 ";
			$sql = $sql . "INNER JOIN HIST_TWITTER_ACCOUNTS T2 ON T1.DS_REST_TWITTER = T2.DS_SCREEN_NAME ";
			$sql = $sql . "WHERE T1.ID_RESTAURANT = '" . $q . "' AND T2.DT_TIMESTAMP > (NOW() - INTERVAL " . $days_itervalo . " DAY) ";
			$sql = $sql . "ORDER BY T2.DT_TIMESTAMP ASC ";
			
			$result = $oDB->select($sql);
			
			$result_arr_followers = array();
			$days =($days_itervalo-1)*(-1);
			$hay_datos=false;
			while($row = mysqli_fetch_row($result))
			{
				$hay_datos=true;
				$dt_score = date_create($row[0]);
				while(date("m.d.y",strtotime("$days day"))!=$dt_score->format('m.d.y')){
					array_push($result_arr_followers,"0");
					$days = $days + 1;	
				}
				
				array_push($result_arr_followers,$row[1]);
				
				$days = $days + 1;
				
			}
			while($days<1 && $hay_datos){
				array_push($result_arr_followers,"0");	
				$days = $days + 1;
			}
			
			array_push($result_arr,$result_arr_fechas);
			array_push($result_arr,$result_arr_values);
			array_push($result_arr,$result_arr_followers);
			
			print json_encode($result_arr, JSON_NUMERIC_CHECK);
			
		}
		if($type_graph == "TWITTER_USERS"){
			$days_intervalo = 0;
			if($intervalo == '7D')
				$days_intervalo = 8;
			if($intervalo == '15D')
				$days_intervalo = 16;
			if($intervalo == '30D')
				$days_intervalo = 31;
			if($intervalo == '3M')
				$days_intervalo = 91;
			if($intervalo == '6M')
				$days_intervalo = 181;
			if($intervalo == '1A')
				$days_intervalo = 366;
			$sql = "SELECT  T2.DS_SCREEN_NAME, T6.DS_NAME, T6.DS_PROFILE_IMAGE_URL, COUNT(*) as NRO_TWEETS ";
			$sql = $sql . "FROM MAST_TWITTER_TWEETS T2 ";
			$sql = $sql . "INNER JOIN MAST_TWITTER_MENTIONS T3 ON T2.ID_TWEET = T3.ID_TWEET ";
			$sql = $sql . "INNER JOIN MAST_TWITTER_ACCOUNTS T4 ON T4.ID_USER_TW = T3.ID_TARGET_USER ";
			$sql = $sql . "INNER JOIN MAST_RESTAURANTS T5 ON T5.DS_REST_TWITTER = T4.DS_SCREEN_NAME ";
			$sql = $sql . "INNER JOIN MAST_TWITTER_USERS T6 ON T6.DS_SCREEN_NAME = T2.DS_SCREEN_NAME ";
			$sql = $sql . "WHERE T5.ID_RESTAURANT = '" . $q . "' AND T2.DT_CREATED_AT > (NOW() - INTERVAL " . $days_intervalo . " DAY) ";
			$sql = $sql . "GROUP BY T2.DS_SCREEN_NAME, T6.DS_NAME, T6.DS_PROFILE_IMAGE_URL ";
			$sql = $sql . "ORDER BY NRO_TWEETS DESC, T6.DS_NAME ASC LIMIT 3 ";
			
			$result = $oDB->select($sql);
			
			$result_des = array();
			while($row = mysqli_fetch_row($result))
			{
				$resto_arr = array();
				$resto_arr['screen_name'] = $row[0];
				$resto_arr['name'] = $row[1];
				$resto_arr['URL'] = $row[2];
				array_push($result_des,$resto_arr);
			}
			
			print json_encode($result_des, JSON_NUMERIC_CHECK);
		}
		if($type_graph == "FACEBOOK_STATS"){
			$days_intervalo = 0;
			if($intervalo == '7D')
				$days_intervalo = 8;
			if($intervalo == '15D')
				$days_intervalo = 16;
			if($intervalo == '30D')
				$days_intervalo = 31;
			if($intervalo == '3M')
				$days_intervalo = 91;
			if($intervalo == '6M')
				$days_intervalo = 181;
			if($intervalo == '1A')
				$days_intervalo = 366;
			$sql = "SELECT DATE_FORMAT(T3.DT_CREATED_TIME, '%Y-%m-%d'), COUNT(*) ";
			$sql = $sql . "FROM MAST_RESTAURANTS T1 ";
			$sql = $sql . "INNER JOIN MAST_FACEBOOK_ACCOUNTS T2 ON T1.DS_REST_FACEBOOK = T2.DS_USER_FB ";
			$sql = $sql . "INNER JOIN  MAST_FACEBOOK_POSTS T3 ON T2.ID_USER_FB = T3.ID_FB_ACCOUNT ";
			$sql = $sql . "WHERE ";
			$sql = $sql . "T1.ID_RESTAURANT = '" . $q . "' AND DATE_FORMAT(T3.DT_CREATED_TIME, '%Y-%m-%d') > (NOW() - INTERVAL " . $days_intervalo . " DAY) ";
			$sql = $sql . "GROUP BY DATE_FORMAT(T3.DT_CREATED_TIME, '%Y-%m-%d') ";
			$sql = $sql . "ORDER BY DT_CREATED_TIME ASC ";
			
			$result = $oDB->select($sql);
			
			$result_arr = array();
			$result_arr_values = array();
			$result_arr_fechas = array();
			$days =($days_intervalo-1)*(-1);
			while($row = mysqli_fetch_row($result))
			{
				
				$dt_score = date_create($row[0]);
				
				while(date("m.d.y",strtotime("$days day"))!=$dt_score->format('m.d.y')){
					
					array_push($result_arr_values,"0");	
					array_push($result_arr_fechas,date("d/m",strtotime("$days day")));
					$days = $days + 1;
				}
				
				
				array_push($result_arr_values,$row[1]);
				array_push($result_arr_fechas,$dt_score->format('d/m'));
				
				$days = $days + 1;
				
			}
			
			while($days<1){
				array_push($result_arr_values,"0");	
				array_push($result_arr_fechas,date("d/m",strtotime("$days day")));
				$days = $days + 1;
			}
			
			$sql = "SELECT DATE_FORMAT(T3.DT_TIMESTAMP, '%Y-%m-%d'), MAX(T3.NUM_LIKES) ";
			$sql = $sql . "FROM MAST_RESTAURANTS T1 ";
			$sql = $sql . "INNER JOIN MAST_FACEBOOK_ACCOUNTS T2 ON T1.DS_REST_FACEBOOK = T2.DS_USER_FB ";
			$sql = $sql . "INNER JOIN HIST_FACEBOOK_ACCOUNTS T3 ON T2.ID_USER_FB = T3.ID_USER_FB ";
			$sql = $sql . "WHERE ";
			$sql = $sql . "T1.ID_RESTAURANT = '" . $q . "' AND DATE_FORMAT(T3.DT_TIMESTAMP, '%Y-%m-%d') > (NOW() - INTERVAL " . $days_intervalo . " DAY) ";
			$sql = $sql . "GROUP BY DATE_FORMAT(T3.DT_TIMESTAMP, '%Y-%m-%d') ";
			$sql = $sql . "ORDER BY DT_TIMESTAMP ASC ";
			
			$result = $oDB->select($sql);
			
			$result_arr_fans = array();
			$days =($days_intervalo-1)*(-1);
			$fans_ant = 0;
			while($row = mysqli_fetch_row($result))
			{
				
				$dt_score = date_create($row[0]);
				
				while(date("m.d.y",strtotime("$days day"))!=$dt_score->format('m.d.y')){
					
					array_push($result_arr_fans,$fans_ant);
					$days = $days + 1;
				}
				
				
				array_push($result_arr_fans,$row[1]);
				$fants_ant = $row[1];
				$days = $days + 1;
				
			}
			
			
			array_push($result_arr,$result_arr_fechas);
			array_push($result_arr,$result_arr_values);
			array_push($result_arr,$result_arr_fans);
			
			
			$sql = "SELECT SUM(T2.NUM_LIKES), SUM(T2.NUM_COMMENTS), SUM(T2.NUM_SHARES) ";
			$sql = $sql . "FROM MAST_FACEBOOK_ACCOUNTS T1 ";
			$sql = $sql . "INNER JOIN MAST_FACEBOOK_POSTS T2 ON T1.ID_USER_FB=T2.ID_FB_ACCOUNT ";
			$sql = $sql . "INNER JOIN MAST_RESTAURANTS T3 ON T3.DS_REST_FACEBOOK = T1.DS_USER_FB ";
			$sql = $sql . "WHERE T3.ID_RESTAURANT='" . $q . "' AND T2.DT_CREATED_TIME >= (NOW() - INTERVAL " . $days_intervalo . " DAY) ";
			
			$result = $oDB->select($sql);
			$result_facebook_stats = array();
			while($row = mysqli_fetch_row($result))
			{
				$result_facebook_stats['num_likes'] = $row[0];
				$result_facebook_stats['num_comments'] = $row[1];
				$result_facebook_stats['num_shares'] = $row[2];
			}
			array_push($result_arr,$result_facebook_stats);
			
			print json_encode($result_arr, JSON_NUMERIC_CHECK);
			
		}
		
		if($type_graph == "REVIEWS_RANKING"){
			
			$sql = "SELECT ID_PARAMETER, NUM_VALUE ";
			$sql = $sql . "FROM LAST_VALUES_RESTAURANT ";
			$sql = $sql . "WHERE ID_RESTAURANT='" . $q . "' AND ID_PARAMETER LIKE 'VAR_POSTS_" . $intervalo . "_%'";
			
			$result = $oDB->select($sql);
			
			$result_arr = array();
			$result_des = array();
			$result_values = array();
			$esta_en_ranking = false;
			while($row = mysqli_fetch_row($result))
			{
				if(strpos($row[0],'ATRAPALO')>0)
					array_push($result_des,'Atrapalo');
				if(strpos($row[0],'RESTALO')>0)
					array_push($result_des,'Restalo');
				if(strpos($row[0],'SALIR')>0)
					array_push($result_des,'Salir');
				if(strpos($row[0],'TRIPADVISOR')>0)
					array_push($result_des,'TripAdvisor');
				if(strpos($row[0],'YELP')>0)
					array_push($result_des,'Yelp');
				if(strpos($row[0],'TENEDOR')>0)
					array_push($result_des,'El Tenedor');	
				array_push($result_values,$row[1]);
			}
			
			array_push($result_arr,$result_des);
			array_push($result_arr,$result_values);
			
			print json_encode($result_arr, JSON_NUMERIC_CHECK);
		}
		
		if($type_graph == "REVIEWS_BY_CATEGORY"){
			$days = 0;
			if($intervalo == '7D')
				$days = 8;
			if($intervalo == '15D')
				$days = 16;
			if($intervalo == '30D')
				$days = 31;		
			if($intervalo == '3M')
				$days = 91;
			if($intervalo == '6M')
				$days = 181;
			if($intervalo == '1A')
				$days = 366;	
			$sql = "SELECT DT_SCORE, AVG_SCORE_CUISINE, AVG_SCORE_AMBIENT, AVG_SCORE_VALUE_PRICE, AVG_SCORE_SERVICE ";
			$sql = $sql . "FROM HIST_SCORE_REVIEWS ";
			$sql = $sql . "WHERE ID_RESTAURANT = '" . $q . "' AND DT_SCORE >= (NOW() - INTERVAL " . $days . " DAY) ";
			$sql = $sql . "ORDER BY DT_SCORE ASC ";
			
			$result = $oDB->select($sql);
			
			$result_arr = array();
			$result_days = array();
			$result_cocina = array();
			$result_ambient = array();
			$result_value_price = array();
			$result_service = array();
			
			while($row = mysqli_fetch_row($result))
			{
				$dt_score = date_create($row[0]);
				array_push($result_days,$dt_score->format('d/m'));
				array_push($result_cocina,$row[1]);
				array_push($result_ambient,$row[2]);
				array_push($result_value_price,$row[3]);
				array_push($result_service,$row[4]);
			}
			
			array_push($result_arr,$result_days);
			array_push($result_arr,$result_cocina);
			array_push($result_arr,$result_ambient);
			array_push($result_arr,$result_value_price);
			array_push($result_arr,$result_service);									
			
			print json_encode($result_arr, JSON_NUMERIC_CHECK);
		}
		
		if($type_graph == "BEST_AND_WORST"){
			$sql = "SELECT ID_PARAMETER, DT_DATE, DS_COMMENT ";
			$sql = $sql . "FROM LAST_VALUES_RESTAURANT ";
			$sql = $sql . "INNER JOIN MAST_COMMENTS ON ";
			$sql = $sql . "DS_VALUE = ID_COMMENT ";
			$sql = $sql . "WHERE (ID_PARAMETER = 'BEST_DAY_" .  $intervalo . "' OR ID_PARAMETER = 'WORST_DAY_" . $intervalo . "') AND ";
			$sql = $sql . "ID_RESTAURANT = " . $q . " ";
			$sql = $sql . "ORDER BY ID_PARAMETER ASC ";
			
			$result = $oDB->select($sql);
			
			$result_arr = array();
			
			while($row = mysqli_fetch_row($result))
			{
				$result_best_or_worst = array();
				if(strpos($row[0],'EST_DAY')>0)
					$result_best_or_worst['best_worst'] = 'BEST_DAY';
				else
					$result_best_or_worst['best_worst'] = 'WORST_DAY';
				$dt_score = date_create($row[1]);
				$result_best_or_worst['date'] = $dt_score->format('d/m');
				$result_best_or_worst['message'] = $row[2];
				
				array_push($result_arr,$result_best_or_worst);
			}
			
			print json_encode($result_arr, JSON_NUMERIC_CHECK);
			
		}
		
		if($type_graph == "REVIEWS_SENTIMENTAL"){
			$days = 0;
			if($intervalo == '7D')
				$days = 8;
			if($intervalo == '15D')
				$days = 16;
			if($intervalo == '30D')
				$days = 31;		
			if($intervalo == '3M')
				$days = 91;
			if($intervalo == '6M')
				$days = 181;
			if($intervalo == '1A')
				$days = 366;	
			$sql = "SELECT DT_SCORE, NUM_POSTS_OK, NUM_POSTS_NOK, NUM_POSTS_NEUTRAL ";
			$sql = $sql . "FROM HIST_SCORE_REVIEWS ";
			$sql = $sql . "WHERE ID_RESTAURANT = " . $q . " AND DT_SCORE >= (NOW() - INTERVAL " . $days . " DAY) ";
			$sql = $sql . "ORDER BY DT_SCORE ASC ";
			
			$result = $oDB->select($sql);
			
			$result_arr = array();
			$result_days = array();
			$result_pos = array();
			$result_neg = array();
			$result_neu = array();
			
			while($row = mysqli_fetch_row($result))
			{
				$dt_score = date_create($row[0]);
				array_push($result_days,$dt_score->format('d/m'));
				array_push($result_pos,$row[1]);
				array_push($result_neg,$row[2]);
				array_push($result_neu,$row[3]);
			}
			
			array_push($result_arr,$result_days);
			array_push($result_arr,$result_pos);
			array_push($result_arr,$result_neg);
			array_push($result_arr,$result_neu);								
			
			print json_encode($result_arr, JSON_NUMERIC_CHECK);
		}
		
		if($type_graph == "IRESTAURANTING_MODULES"){
			$sql = "SELECT ID_MODULE ";
			$sql = $sql . "FROM MAST_USERS_MODULES T1 INNER JOIN MAST_USERS T2 ON T1.ID_USER = T2.ID_USER ";
			$sql = $sql . "WHERE T2.DS_USER_ID = '" . $_SESSION['username'] . "' AND T1.ID_RESTAURANT = '" . $q . "'";
			
			$result = $oDB->select($sql);
			$result_arr = array();
			while($row = mysqli_fetch_row($result))
			{	
				if($row[0] == 10) // 4SQUARE
					$result_arr["foursquare"] = 1;
			}
			
			print json_encode($result_arr, JSON_NUMERIC_CHECK);
		}
		
		if($type_graph == "IRESTAURANTING_GENERAL"){
			$sql = "SELECT DS_REST_NAME, DS_REST_ADDRESS, ";
			$sql = $sql . "DS_REST_URL, DS_REST_FACEBOOK, DS_REST_TWITTER, NUM_REST_LATITUDE, NUM_REST_LONGITUDE, T2.ID_LOCALITY, T2.DS_LOCALITY_1, ";
			$sql = $sql . "T1.ID_COUNTRY_3, T1.ID_ADMIN_AREA_L1, T1.ID_ADMIN_AREA_L2  ";
			$sql = $sql . "FROM MAST_RESTAURANTS T1 INNER JOIN MAST_LOCALITIES T2 ON T1.ID_LOCALITY = T2.ID_LOCALITY AND T1.ID_COUNTRY_3 = T2.ID_COUNTRY_3 AND ";
			$sql = $sql . " T1.ID_ADMIN_AREA_L1 = T2.ID_ADMIN_AREA_L1 AND T1.ID_ADMIN_AREA_L2 = T2.ID_ADMIN_AREA_L2 ";			
			$sql = $sql . "WHERE ID_RESTAURANT='" . $q . "'";
			
			$result = $oDB->select($sql);
			
			while($row = mysqli_fetch_row($result))
			{			
				$result_arr = array('description'=>$row[0], 'address'=>$row[1]);
				if(is_null($row[2]))
					$result_arr["rest_url"] = "";
				else
					$result_arr["rest_url"] = $row[2];
					
				if(is_null($row[3]))
					$result_arr["rest_facebook"] = "";
				else
					$result_arr["rest_facebook"] = $row[3];
				if(is_null($row[4]))
					$result_arr["rest_twitter"] = "";
				else
					$result_arr["rest_twitter"] = $row[4];
				if(is_null($row[5]))
					$result_arr["rest_latitude"] = 0;
				else
					$result_arr["rest_latitude"] = $row[5];
				if(is_null($row[6]))
					$result_arr["rest_longitude"] = 0;
				else
					$result_arr["rest_longitude"] = $row[6];
				$id_rest_city = $row[7];
				$result_arr["ds_rest_city"] = $row[8];
				$id_country = $row[9];
				$id_admin_area_1 = $row[10];
				$id_admin_area_2 = $row[11];
			}
			
			$sql = "SELECT ID_PARAMETER, NUM_VALUE ";
			$sql = $sql . "FROM LAST_VALUES_RESTAURANT ";
			$sql = $sql . "WHERE ID_RESTAURANT = '" . $q . "' AND (ID_PARAMETER LIKE 'NUM%' OR ID_PARAMETER LIKE 'VAR_SCORE_%_" . $intervalo . "' ";
			$sql = $sql . "OR ID_PARAMETER='VAR_POSTS_" . $intervalo . "' OR ID_PARAMETER LIKE 'VAR_FOLLOWERS%" . $intervalo . "')";
					
			$result = $oDB->select($sql);
			
			while($row = mysqli_fetch_row($result))
			{			
				if($row[0]=="NUM_SCORE_ACTIVITY")
					$result_arr["irest_activity"] = number_format($row[1],1,',','');
				if($row[0]=="NUM_SCORE_GLOBAL")
					$result_arr["irest_scoring"] = number_format($row[1],1,',','');
				if($row[0]=="NUM_SCORE_OPINIONS")
					$result_arr["irest_opinions"] = number_format((float)$row[1],1,',','');
				if($row[0]=="NUM_SCORE_PRESENCE")
					$result_arr["irest_presence"] = number_format($row[1],1,',','');
				if($row[0]=="NUM_RANK_CITY")
					$result_arr["num_ranking"] = $row[1];
				if($row[0]=="VAR_SCORE_ACTIVITY_" . $intervalo)
					$result_arr["irest_activity_var"] = $row[1];
				if($row[0]=="VAR_SCORE_GLOBAL_" . $intervalo)
					$result_arr["irest_scoring_var"] = $row[1];
				if($row[0]=="VAR_SCORE_OPINIONS_" . $intervalo)
					$result_arr["irest_opinions_var"] = $row[1];
				if($row[0]=="VAR_SCORE_PRESENCE_" . $intervalo)
					$result_arr["irest_presence_var"] = $row[1];
				if($row[0]=="VAR_POSTS_" . $intervalo)
					$result_arr["irest_var_posts"] = $row[1];
				if($row[0]=="VAR_FOLLOWERS_FACEBOOK_" . $intervalo)
					$result_arr["irest_var_fans_facebook"] = $row[1];
				if($row[0]=="VAR_FOLLOWERS_TWITTER_" . $intervalo)
					$result_arr["irest_var_followers_twitter"] = $row[1];
				
			}
			
			$sql = "SELECT COUNT(*) ";
			$sql = $sql . "FROM MAST_RESTAURANTS WHERE ID_LOCALITY='" . $id_rest_city . "' AND ID_COUNTRY_3='" . $id_country . "' ";
			$sql = $sql . " AND ID_ADMIN_AREA_L1 = '" . $id_admin_area_1 . "' AND ID_ADMIN_AREA_L2 = '" . $id_admin_area_2 ."'";
			
			$result = $oDB->select($sql);
			
			while($row = mysqli_fetch_row($result))
			{			
				$result_arr["cont_rest"] = $row[0];
			}
			
			print json_encode($result_arr, JSON_NUMERIC_CHECK);
		}
		
		if($type_graph == "CHANNELS_PRESENCE"){
			$sql = "SELECT ID_PARAMETER, NUM_VALUE, DS_VALUE ";
			$sql = $sql . "FROM LAST_VALUES_RESTAURANT ";
			$sql = $sql . "WHERE ID_RESTAURANT = '" . $q . "' AND ";
			$sql = $sql . "ID_PARAMETER LIKE 'PRESENCE%'";
			
			$result = $oDB->select($sql);
			$result_arr = array();
			while($row = mysqli_fetch_row($result))
			{	
				$result_values = array('id_parameter'=>$row[0], 'presence'=>$row[1], 'url'=>$row[2]);		
				array_push($result_arr, $result_values);
			}
			
			print json_encode($result_arr, JSON_NUMERIC_CHECK);
			
		}
		if($type_graph == "IRESTAURANTING_SCORING"){ 
			$days = 0;
			if($intervalo == '7D')
				$days = 8;
			if($intervalo == '15D')
				$days = 16;
			if($intervalo == '30D')
				$days = 31;
			if($intervalo == '3M')
				$days = 91;
			if($intervalo == '6M')
				$days = 181;
			if($intervalo == '1A')
				$days = 366;		
			$sql = "SELECT DT_SCORE, NUM_SCORE_GLOBAL, NUM_SCORE_PRESENCE, NUM_SCORE_OPINIONS, NUM_SCORE_ACTIVITY  FROM HIST_SCORE_GLOBAL ";
			$sql = $sql . "WHERE  DT_SCORE > (NOW() - INTERVAL " . $days . " DAY) AND ID_RESTAURANT= '" . $q . "' ";
			$sql = $sql . "ORDER BY DT_SCORE ASC ";
			
			$result = $oDB->select($sql);
			
			$result_arr = array();
			$result_arr_values = array();
			$result_arr_values_pre = array();
			$result_arr_values_opi = array();
			$result_arr_values_act = array();
			$result_arr_fechas = array();
			while($row = mysqli_fetch_row($result))
			{
				//$datetime = strtotime($row[0]);
				$dt_score = date_create($row[0]);
				
				array_push($result_arr_values,$row[1]);				
				array_push($result_arr_values_pre,$row[2]);	
				array_push($result_arr_values_opi,$row[3]);	
				array_push($result_arr_values_act,$row[4]);	
				//array_push($result_arr_fechas,$dt_score->format('d/m'););
				array_push($result_arr_fechas,$dt_score->format('d/m'));
			}
			
			array_push($result_arr,$result_arr_fechas);
			array_push($result_arr,$result_arr_values);
			array_push($result_arr,$result_arr_values_pre);
			array_push($result_arr,$result_arr_values_opi);
			array_push($result_arr,$result_arr_values_act);
			
			print json_encode($result_arr, JSON_NUMERIC_CHECK);
		}
		if($type_graph == "IRESTAURANTING_CERCANOS"){
			$sql = "SELECT NUM_REST_LATITUDE, NUM_REST_LONGITUDE ";
			$sql = $sql . "FROM MAST_RESTAURANTS ";
			$sql = $sql . "WHERE ID_RESTAURANT = '" . $q . "' ";

			$result = $oDB->select($sql);
			
			$latitud = 0;
			$longitud = 0;
			while($row = mysqli_fetch_row($result)){
				$latitud = $row[0];
				$longitud = $row[1];
			}
			
			$sql = "SELECT DS_REST_NAME, MAST_RESTAURANTS.ID_RESTAURANT, NUM_REST_LATITUDE, NUM_REST_LONGITUDE, NUM_VALUE, 3956 * 2 * ";
			$sql = $sql . "ASIN(SQRT( ";
			$sql = $sql . "POWER(SIN((" . $latitud . " - abs(NUM_REST_LATITUDE)) * pi()/180 / 2),2) + ";
			$sql = $sql . "COS(" . $latitud . " * pi()/180 ) * ";
			$sql = $sql . "COS(abs(NUM_REST_LATITUDE) *  pi()/180) * ";
			$sql = $sql . "POWER(SIN((" . $longitud . " - NUM_REST_LONGITUDE) *  pi()/180 / 2), 2) ";
			$sql = $sql . ")) as distance ";
			$sql = $sql . "FROM MAST_RESTAURANTS INNER JOIN LAST_VALUES_RESTAURANT ON ";
			$sql = $sql . "MAST_RESTAURANTS.ID_RESTAURANT = LAST_VALUES_RESTAURANT.ID_RESTAURANT ";
			$sql = $sql . "WHERE ";
			$sql = $sql . "ID_PARAMETER = 'NUM_SCORE_GLOBAL' ";
			$sql = $sql . "having distance < 0.2 ";
			$sql = $sql . "ORDER BY NUM_VALUE DESC limit 11; ";
			
			$result = $oDB->select($sql);
			$result_arr = array();
			
			while($row = mysqli_fetch_row($result)){
				$result_values = array();
				if($row[1] != $q){
					$result_values['description'] = $row[0];
					$result_values['id'] = $row[1];
					$result_values['latitude'] = $row[2];
					$result_values['longitude'] = $row[3];
					$result_values['scoring'] = number_format($row[4],1,',','');
					$result_values['distance'] = $row[5];
					array_push($result_arr,$result_values);	
				}
				
				
			}
			
			print json_encode($result_arr, JSON_NUMERIC_CHECK);	
		}
		if($type_graph == "IRESTAURANTING_TOP20"){
			$sql = "SELECT T1.ID_RESTAURANT, NUM_VALUE, DS_REST_NAME, DS_REST_ADDRESS ";
			$sql = $sql . "FROM LAST_VALUES_RESTAURANT T1 INNER JOIN MAST_RESTAURANTS T2 ";
			$sql = $sql . "ON T1.ID_RESTAURANT = T2.ID_RESTAURANT AND ID_PARAMETER = 'NUM_SCORE_GLOBAL' ";
			$sql = $sql . "ORDER BY NUM_VALUE DESC ";
			$sql = $sql . "LIMIT 20 ";
			
			$result = $oDB->select($sql);
			
			$result_arr = array();
			
			while($row = mysqli_fetch_row($result)){
				$result_values = array();
				$result_values['id'] = $row[0];
				$result_values['scoring'] = number_format($row[1],1,',','');
				$result_values['name'] = $row[2];
				$result_values['address'] = $row[3];
				array_push($result_arr,$result_values);
			}
			
			print json_encode($result_arr, JSON_NUMERIC_CHECK);	
		}
		if($type_graph == "FOURSQUARE_STATS"){
			$days_intervalo = 0;
			if($intervalo == '7D')
				$days_intervalo = 9;
			if($intervalo == '15D')
				$days_intervalo = 17;
			if($intervalo == '30D')
				$days_intervalo = 32;
			if($intervalo == '3M')
				$days_intervalo = 92;
			if($intervalo == '6M')
				$days_intervalo = 182;
			if($intervalo == '1A')
				$days_intervalo = 367;
			$sql = "SELECT T2.DT_UPDATE, T2.NUM_SCORE, T2.NUM_CHECK_INS ";
			$sql = $sql . "FROM MAST_REST_FOURSQUARE T1 INNER JOIN HIST_REST_FOURSQUARE T2 ON T1.ID_RESTAURANT = T2.ID_RESTAURANT ";
			$sql = $sql . "WHERE T1.ID_RESTAURANT = '" . $q . "' AND DATE_FORMAT(T2.DT_UPDATE, '%Y-%m-%d') > (NOW() - INTERVAL '" . $days_intervalo . "' DAY) ";
			$sql = $sql . "ORDER BY T2.DT_UPDATE ASC ";
			
			$result = $oDB->select($sql);
			
			$result_arr = array();
			$result_arr_values = array();
			$result_arr_fechas = array();
			$result_arr_checkins = array();
			$days =($days_intervalo-1)*(-1);
			$first_time = true;
			$checkins_ant = 0;
			while($row = mysqli_fetch_row($result))
			{
				
				$dt_score = date_create($row[0]);
				
				while(date("m.d.y",strtotime("$days day"))!=$dt_score->format('m.d.y')){
					
					array_push($result_arr_values,"0");	
					array_push($result_arr_checkins,"0");	
					array_push($result_arr_fechas,date("d/m",strtotime("$days day")));
					$days = $days + 1;
				}
				
				if($first_time){
					$checkins_ant = $row[2];
					$first_time = false;
					array_push($result_arr_values,"0");	
					array_push($result_arr_checkins,"0");	
					array_push($result_arr_fechas,date("d/m",strtotime("$days day")));
				}
				else {
					array_push($result_arr_values,$row[1]);
					array_push($result_arr_checkins,$row[2]-$checkins_ant);	
					array_push($result_arr_fechas,$dt_score->format('d/m'));
					$checkins_ant = $row[2];
				}
				
				$days = $days + 1;
				
			}
			
			while($days<0){
				array_push($result_arr_values,"0");	
				array_push($result_arr_checkins,"0");	
				array_push($result_arr_fechas,date("d/m",strtotime("$days day")));
				$days = $days + 1;
			}
			
			
			array_push($result_arr,$result_arr_fechas);
			array_push($result_arr,$result_arr_values);
			array_push($result_arr,$result_arr_checkins);
			
			$sql = "SELECT T1.DS_URL_FOURSQUARE, T2.ID_USER, T2.DS_NAME, T2.URL_PHOTO, T2.URL_USER  ";
			$sql = $sql . "FROM MAST_REST_FOURSQUARE T1 INNER JOIN MAST_FOURSQUARE_USERS T2 ON T1.ID_USER_MAYOR = T2.ID_USER ";
			$sql = $sql . "WHERE T1.ID_RESTAURANT = '" . $q . "'";
			
			
			$result = $oDB->select($sql);
			$result_foursquare_stats = array();
			while($row = mysqli_fetch_row($result))
			{
				$result_foursquare_stats['url_foursquare'] = $row[0];
				$result_foursquare_stats['id_user'] = $row[1];
				$result_foursquare_stats['name'] = $row[2];
				$result_foursquare_stats['url_photo'] = $row[3];
				$result_foursquare_stats['url_user'] = $row[4];
			}
			array_push($result_arr,$result_foursquare_stats);
			
			print json_encode($result_arr, JSON_NUMERIC_CHECK);
			
		}
		
	}
	catch(Exception $e){
		echo $e;
	}	
	



?>